#!/usr/bin/env python
# Copyright 2019 The Clspv Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os.path
import re
import binascii

def main():
    import argparse
    parser = argparse.ArgumentParser(description='Generate baked header file')

    parser.add_argument('--input-header-file', metavar='<path>',
            type=str, required=True, help='input OpenCL C header file')
    parser.add_argument('--input-base-file', metavar='<path>',
            type=str, required=False, help='input base OpenCL C header file')
    parser.add_argument('--output-file', metavar='<path>',
            type=str, required=True, help='output file')
    parser.add_argument('--header-var', type=str, required=True,
            help='Header variable name')
    parser.add_argument('--header-size-var', type=str, required=True,
            help='Header size variable name')
    parser.add_argument('--base-var', type=str, required=False,
            help='Base header variable name')
    parser.add_argument('--base-size-var', type=str, required=False,
            help='Base header size variable name')

    args = parser.parse_args()

    with open(args.output_file, "w") as output:
        output.write("// Copyright 2017 The Clspv Authors. All rights reserved.\n")
        output.write("//\n")
        output.write("// Licensed under the Apache License, Version 2.0 (the \"License\");\n")
        output.write("// you may not use this file except in compliance with the License.\n")
        output.write("// You may obtain a copy of the License at\n")
        output.write("//\n")
        output.write("//     http://www.apache.org/licenses/LICENSE-2.0\n")
        output.write("//\n")
        output.write("// Unless required by applicable law or agreed to in writing, software\n")
        output.write("// distributed under the License is distributed on an \"AS IS\" BASIS,\n")
        output.write("// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n")
        output.write("// See the License for the specific language governing permissions and\n")
        output.write("// limitations under the License.\n")
        output.write("// THIS FILE IS AUTOGENERATED - DO NOT EDIT!\n")

        header_blocker = args.output_file.upper();
        header_blocker = re.sub("[^A-Z]", "_", header_blocker)
        output.write("#ifndef %s\n" % header_blocker)
        output.write("#define %s\n" % header_blocker)
        output.write("#ifdef __cplusplus\n")
        output.write("extern \"C\" {\n")
        output.write("#endif\n\n")

        # Write the contents of the array for the OpenCL header.
        header_size = os.stat(args.input_header_file).st_size + 1
        output.write("static const unsigned int %s = %d;\n"
                % (args.header_size_var, header_size))
        output.write("static const char %s[%s] = {\n"
                % (args.header_var, args.header_size_var))
        with open(args.input_header_file, "rb") as input:
            byte = input.read(1)
            while byte != b"":
                output.write("  '\\x%s',\n"
                        % binascii.hexlify(byte).decode('utf-8'))
                byte = input.read(1)
        output.write("  '\\0'\n};\n\n")

        if args.input_base_file:
            # Write the contents of the array for the OpenCL base header.
            base_size = os.stat(args.input_base_file).st_size + 1
            output.write("static const unsigned int %s = %d;\n"
                    % (args.base_size_var, base_size))
            output.write("static const char %s[%s] = {\n"
                    % (args.base_var, args.base_size_var))
            with open(args.input_base_file, "rb") as input:
                byte = input.read(1)
                while byte != b"":
                    output.write("  '\\x%s',\n"
                            % binascii.hexlify(byte).decode('utf-8'))
                    byte = input.read(1)
            output.write("  '\\0'\n};\n\n")

        output.write("\n\n")
        output.write("#ifdef __cplusplus\n")
        output.write("}\n")
        output.write("#endif\n")
        output.write("#endif//%s\n\n" % header_blocker)

if __name__ == '__main__':
    main()
